package proc

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/bitly/go-simplejson"
	"github.com/hu17889/go_spider/core/common/page"
	corerequest "github.com/hu17889/go_spider/core/common/request"
	"github.com/hu17889/go_spider/core/common/util"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
	"net/http"
	"strconv"
	"strings"
	"tesou.io/platform/poem-parent/poem-api/common/base"
	"tesou.io/platform/poem-parent/poem-api/module/core/enums"
	"tesou.io/platform/poem-parent/poem-api/module/core/pojo"
	"tesou.io/platform/poem-parent/poem-core/module/core/service"
	"time"
)

//TODO 遇到解析 jsonp2json 问题
type JiemianProcesser struct {
	JiemianService    service.JiemianService
	SpiderUrl string
	cookies []*http.Cookie
}

func GetJiemianProcesser() *JiemianProcesser {
	return &JiemianProcesser{}
}

func (this *JiemianProcesser) Startup() {
	newSpider := spider.NewSpider(this, "JiemianProcesser")
	fullUrlOne := strings.Replace(this.SpiderUrl, "${lasttime}", strconv.FormatInt(time.Now().Unix(), 10) , 1)
	//newSpider = newSpider.AddUrl(this.SpiderUrl, "html")
	base.Log.Info("start URL:,", fullUrlOne)
	hder := make(http.Header)
	hder.Add("User-Agent", `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36`)
	hder.Add("Upgrade-Insecure-Requests", `1`)
	hder.Add("Host", `www.jiemian.com`)

	req := corerequest.NewRequest(fullUrlOne, "text", "jiemianindex", "GET", "", hder, nil, nil,nil)
	newSpider.AddRequest(req)

	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
	newSpider.SetThreadnum(1).Run()
}

func (this *JiemianProcesser) Process(p *page.Page) {
	request := p.GetRequest()
	if !p.IsSucc() {
		base.Log.Info("URL:,", request.Url, p.Errormsg())
		return
	}
	//var allData []map[string]interface{}
	zhi_list_slice := make([]interface{}, 0)
	zhi_list_update_slice := make([]interface{}, 0)
	level := enums.GetListLevel("界面")
	//jQuery1102006110913015600472_1624333425484(
	jsonp:= p.GetBodyStr()
	/*if e!=nil{
		base.Log.Info("err:,", e)
	}*/
	tmpstr := util.JsonpToJson(jsonp)
	base.Log.Info("tmpstr:,", tmpstr)

	js, err2 := simplejson.NewJson([]byte(tmpstr))
	if err2 != nil {
		base.Log.Info("err:,", err2)
	}
	base.Log.Info("js:,", js)

	v, _ := zhToUnicode([]byte(jsonp))
	fmt.Println(string(v))
	base.Log.Info("v:,", v)

	aaaword := strings.Split(string(v), "推荐阅读开始 -->")[1]  //, "guestFeeds"
	bbbword := strings.Split(aaaword, "<!-- JMedia结束")[0]
	bbbword = strings.TrimSpace(bbbword)
	base.Log.Info("bbbword:,", bbbword)
	bbbword,ee := disableEscapeHtml(bbbword)
	if ee!=nil{
		base.Log.Info("ee:,", ee)
	}
	new_p := p.SetBodyStr(bbbword)

	new_p.GetHtmlParser().Find("div").Each(func(i int, selection *goquery.Selection) {
		s := selection.Find("a").First()
		url, boolUrl := s.Attr("href")
		text := s.Text()
		img := selection.Find("img").First()
		img_src, _ := img.Attr("src")
		if len(img_src) < 5{
			img_src, _ = img.Attr("data-original")
		}

		content_src := strings.TrimSpace(selection.Find(".indexs").First().Text())
		author_src := selection.Find("strong").First().Text()
		if len(url) != 0 {
			if boolUrl {
				zhi := new(pojo.List)
				zhi.ListId = strings.Replace(url, "http://jandan.net/p/", "", -1)
				zhi.Image = img_src
				zhi.Author = author_src
				zhi.Content = content_src
				zhi.Title = text
				zhi.SUrl = url
				if len(zhi.Title) > 0 {
					//设置朝代
					zhi.Type = level
					exist := this.JiemianService.Exist(zhi)
					if !exist {
						zhi_list_slice = append(zhi_list_slice, zhi)
					} else {
						zhi_list_update_slice = append(zhi_list_update_slice, zhi)
					}
				}
			}
		}
	})

	//执行入库
	this.JiemianService.SaveList(zhi_list_slice)
	this.JiemianService.ModifyList(zhi_list_update_slice)

}

func (this *JiemianProcesser) Finish() {
	base.Log.Info("zhihu表抓取解析完成 \r\n")

}

func zhToUnicode(raw []byte) ([]byte, error) {
	str, err := strconv.Unquote(strings.Replace(strconv.Quote(string(raw)), `\\u`, `\u`, -1))
	if err != nil {
		return nil, err
	}
	return []byte(str), nil
}
// 去除json中的转义字符
func disableEscapeHtml(data interface{}) (string, error) {
	bf := bytes.NewBuffer([]byte{})
	jsonEncoder := json.NewEncoder(bf)
	jsonEncoder.SetEscapeHTML(false)
	if err := jsonEncoder.Encode(data); err != nil {
		return "", err
	}
	return bf.String(), nil
}